home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Common / d3dinit.bas < prev    next >
BASIC Source File  |  2001-10-08  |  41KB  |  1,108 lines

  1. Attribute VB_Name = "D3DInit"
  2.  
  3. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  4. '
  5. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  6. '
  7. '  File:       D3DInit.bas
  8. '  Content:    VB D3DFramework global initialization module
  9. '
  10. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  11.  
  12. ' DOC:  Use with
  13. ' DOC:        D3DAnimation.cls
  14. ' DOC:        D3DFrame.cls
  15. ' DOC:        D3DMesh.cls
  16. ' DOC:        D3DSelectDevice.frm (optional)
  17. ' DOC:
  18. ' DOC:  Short list of usefull functions
  19. ' DOC:        D3DUtil_Init                  first call to framework
  20. ' DOC:        D3DUtil_LoadFromFile          loads an x-file
  21. ' DOC:        D3DUtil_SetupDefaultScene     setup a camera lights and materials
  22. ' DOC:        D3DUtil_SetupCamera           point camera
  23. ' DOC:        D3DUtil_SetupMediaPath        set directory to load textures from
  24. ' DOC:        D3DUtil_PresentAll            show graphic on the screen
  25. ' DOC:        D3DUtil_ResizeWindowed        resize for windowed modes
  26. ' DOC:        D3DUtil_ResizeFullscreen      resize to fullscreen mode
  27. ' DOC:        D3DUtil_CreateTextureInPool   create a texture
  28.  
  29. Option Explicit
  30.  
  31. Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As Long, ByRef RECT As RECT) As Long
  32. Private Declare Function GetClientRect Lib "user32.dll" (ByVal hwnd As Long, ByRef RECT As RECT) As Long
  33. Private Declare Function SetWindowPos Lib "user32.dll" (ByVal hwnd As Long, ByVal hwndafter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal options As Long) As Long
  34. Private Declare Function SetWindowLongA Lib "user32.dll" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal newVal As Long) As Long
  35. Private Declare Function GetWindowLongA Lib "user32.dll" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
  36.  
  37.  
  38. '------------------------------------------------------------------
  39. '  User Defined types
  40. '------------------------------------------------------------------
  41.  
  42. ' DOC: Type that hold info about a display mode
  43. Public Type D3DUTIL_MODEINFO
  44.     lWidth As Long                                  'Screen width in this mode
  45.     lHeight As Long                                 'Screen height in this mode
  46.     format As CONST_D3DFORMAT                       'Pixel format in this mode
  47.     VertexBehavior As CONST_D3DCREATEFLAGS          'Whether this mode does SW or HW vertex processing
  48. End Type
  49.  
  50. ' DOC: Type that hold info about a particular back buffer format
  51. Public Type D3DUTIL_FORMATINFO
  52.     format As CONST_D3DFORMAT
  53.     usage As Long
  54.     bCanDoWindowed As Boolean
  55.     bCanDoFullscreen As Boolean
  56. End Type
  57.  
  58. Public Type D3DUTIL_DEVTYPEINFO
  59.     'List of compatible Modes for this device
  60.     lNumModes As Long
  61.     Modes() As D3DUTIL_MODEINFO
  62.         
  63.     'List of compatible formats for this device
  64.     lNumFormats As Long
  65.     FormatInfo() As D3DUTIL_FORMATINFO
  66.  
  67.     lCurrentMode As Long        'index into modes array of the current mode
  68.     
  69. End Type
  70.  
  71. ' DOC: Type that holds info about adapters installed on the system
  72. Public Type D3DUTIL_ADAPTERINFO
  73.     'Device data
  74.     DeviceType As CONST_D3DDEVTYPE                  'Reference, HAL
  75.     d3dcaps As D3DCAPS8                             'Caps of this device
  76.     sDesc As String                                 'Name of this device
  77.     lCanDoWindowed As Long                          'Whether or not this device can work windowed
  78.                 
  79.     DevTypeInfo(2) As D3DUTIL_DEVTYPEINFO           'format and display mode list for hal=1 for ref=2
  80.     
  81.     d3dai As D3DADAPTER_IDENTIFIER8
  82.             
  83.     DesktopMode As D3DDISPLAYMODE
  84.     
  85.     'CurrentState
  86.     bWindowed As Boolean        'currently in windowed mode
  87.     bReference As Boolean       'currently using reference rasterizer
  88.  
  89.     
  90.     
  91. End Type
  92.  
  93. '------------------------------------------------------------------
  94. ' DOC: Usefull globals
  95. '------------------------------------------------------------------
  96. '
  97. Public g_Adapters() As D3DUTIL_ADAPTERINFO      ' Array of Adapter infos
  98. Public g_lCurrentAdapter As Long                ' current adapter (index into infos)
  99. Public g_lNumAdapters As Long                   ' size of the g_Adapters array
  100. Public g_EnumCallback As Object                 ' object that defines VerifyDevice function
  101.  
  102. Public g_dx As DirectX8                         ' Root objects for DX8
  103. Public g_d3dx As D3DX8                          ' Root objects for d3dx
  104. Public g_d3d As Direct3D8                       ' Root object for d3d
  105. Public g_dev As Direct3DDevice8                 ' D3D device
  106.     
  107.                                                 ' Current state (use as read only)
  108. Public g_d3dCaps As D3DCAPS8                    ' Current caps of g_dev
  109. Public g_devType As CONST_D3DDEVTYPE            ' Current device type (hardware or software)
  110. Public g_behaviorflags As Long                  ' Current VertexProcessing (hardware or software)
  111. Public g_focushwnd As Long                      ' Current focus window handle
  112. Public g_d3dpp As D3DPRESENT_PARAMETERS         ' Current presentation parameters
  113.  
  114.   
  115. Public g_lWindowWidth As Long                   ' backbuffer width of windowed state
  116. Public g_lWindowHeight As Long                  ' backbuffer  height of windowed state
  117. Public g_WindowRect As RECT                     ' size of window (including title bar)
  118.  
  119.  
  120.  
  121. '------------------------------------------------------------------
  122. ' Public Functions
  123. '------------------------------------------------------------------
  124.  
  125. Sub D3DUtil_Destory()
  126.     Dim emptycaps As D3DCAPS8
  127.     Dim emptyrect As RECT
  128.     Dim emptypresent As D3DPRESENT_PARAMETERS
  129.     
  130.     D3DUtil_ReleaseAllTexturesFromPool
  131.     
  132.     Set g_dx = Nothing
  133.     Set g_d3dx = Nothing
  134.     Set g_dev = Nothing
  135.     Set g_EnumCallback = Nothing
  136.     
  137.     g_focushwnd = 0
  138.     g_behaviorflags = 0
  139.     g_lWindowWidth = 0
  140.     g_lWindowHeight = 0
  141.     
  142.     g_d3dCaps = emptycaps
  143.     g_d3dpp = emptypresent
  144.     g_WindowRect = emptyrect
  145.     
  146.     ReDim g_Adapters(0)
  147.     g_lNumAdapters = 0
  148.         
  149. End Sub
  150.  
  151. '-----------------------------------------------------------------------------
  152. 'DOC: D3DUtil_Init
  153. 'DOC:   This function creates the following objects: DirectX8, Direct3D8,
  154. 'DOC:   Direc3DDevice8.
  155. 'DOC:
  156. 'DOC: Params:
  157. 'DOC:   bWindowed       Start in full screen or windowed mode
  158. 'DOC:
  159. 'DOC:   hwnd            starting hwnd to display graphics in
  160. 'DOC:                   This can be changed with a call to Reset if drawing
  161. 'DOC:                   to multiple windows
  162. 'DOC:                   For full screen operation the hwnd will have to belong
  163. 'DOC:                   to a top level window
  164. 'DOC:
  165. 'DOC:   AdapterIndex    The index of the display card to be used (most often 0)
  166. 'DOC:
  167. 'DOC:   ModeIndex       Ignored if bWindowed is TRUE
  168. 'DOC:                   Otherwise and index into the g_adapters(AdapterIndex).Modes
  169. 'DOC:                   array for a given width height and backbuffer format
  170. 'DOC:                   Use D3DUtil_FindDisplayMode to obtain an mode index given
  171. 'DOC:                   a desired height width and backbuffer format
  172. 'DOC:
  173. 'DOC:   CallbackObject  Can be Nothing or an object that has implemented
  174. 'DOC:                   VerifyDevice(usageflags as long ,format as CONST_D3DFORMAT)
  175. 'DOC:
  176. 'DOC:
  177. 'DOC: Remarks:
  178. 'DOC:   caps for the device are passed to VerifyDevice in the g_d3dcaps global
  179. 'DOC:
  180. '-----------------------------------------------------------------------------
  181.  
  182. Public Function D3DUtil_Init(hwnd As Long, bWindowed As Boolean, AdapterIndex As Long, modeIndex As Long, devtype As CONST_D3DDEVTYPE, CallbackObject As Object) As Boolean
  183.     
  184.     On Local Error GoTo errOut
  185.  
  186.     ' Initialize the DirectX8 and d3dx8 objects
  187.     If g_dx Is Nothing Then Set g_dx = New DirectX8
  188.     If g_d3dx Is Nothing Then Set g_d3dx = New D3DX8
  189.     
  190.     ' Create the Direct3D object
  191.     Set g_d3d = g_dx.Direct3DCreate
  192.  
  193.     ' Call the sub that builds a list of available adapters,
  194.     ' adapter device types, and display modes
  195.     Call D3DEnum_BuildAdapterList(CallbackObject)
  196.     
  197.     If bWindowed Then
  198.         D3DUtil_Init = D3DUtil_InitWindowed(hwnd, AdapterIndex, devtype, True)
  199.     Else
  200.         D3DUtil_Init = D3DUtil_InitFullscreen(hwnd, AdapterIndex, modeIndex, devtype, True)
  201.     End If
  202.     
  203.     Exit Function
  204.     
  205. errOut:
  206.     Debug.Print "Failed D3DUtil_Init"
  207. End Function
  208.  
  209.  
  210. '-----------------------------------------------------------------------------
  211. 'DOC: D3DUtil_InitWindowed
  212. 'DOC:   This function creates the following objects: DirectX8, Direct3D8,
  213. 'DOC:   Direc3DDevice8.
  214. 'DOC:
  215. 'DOC: Params:
  216. 'DOC:
  217. 'DOC:   hwnd            starting hwnd to display graphics in
  218. 'DOC:                   This can be changed with a call to Reset if drawing
  219. 'DOC:                   to multiple windows
  220. 'DOC:                   For full screen operation the hwnd will have to belong
  221. 'DOC:                   to a top level window
  222. 'DOC:
  223. 'DOC:   AdapterIndex    The index of the display card to be used (most often 0)
  224. 'DOC:
  225. 'DOC:   DevType         Indicates if user wants HAL (hardware) or REF (software) rendering
  226. 'DOC:
  227. 'DOC:   bTryFallbacks   True if wants function to attempt to fallback to the reference device
  228. 'DOC:                   on faulure and display dialogs to that effect
  229. 'DOC:
  230. '-----------------------------------------------------------------------------
  231.  
  232. Function D3DUtil_InitWindowed(hwnd As Long, AdapterIndex As Long, devtype As CONST_D3DDEVTYPE, bTryFallbacks As Boolean) As Boolean
  233.    
  234.     On Error GoTo errOut
  235.     
  236.         
  237.     
  238.     'save the current adapter
  239.     g_lCurrentAdapter = AdapterIndex
  240.     
  241.     Dim d3ddm As D3DDISPLAYMODE
  242.  
  243.     ' Initialize the present parameters structure
  244.     ' to use 1 back buffer and a 16 bit depth buffer
  245.     ' change the autoDepthStencilFormat if you need stencil bits
  246.     With g_d3dpp
  247.         .BackBufferCount = 1
  248.         .EnableAutoDepthStencil = 1
  249.         .AutoDepthStencilFormat = D3DFMT_D16
  250.         .hDeviceWindow = hwnd
  251.     End With
  252.         
  253.     g_focushwnd = hwnd
  254.     
  255.     Dim rc As RECT
  256.     Call GetClientRect(g_focushwnd, rc)
  257.     g_lWindowWidth = rc.Right - rc.Left
  258.     g_lWindowHeight = rc.bottom - rc.Top
  259.     Call GetWindowRect(g_focushwnd, g_WindowRect)
  260.     
  261.     
  262.     With g_Adapters(g_lCurrentAdapter)
  263.     
  264.         ' If running windowed, set the current desktop format
  265.         ' as the format the device will use.
  266.         ' note the the current mode and backbuffer width and height
  267.         ' information is ignored by d3d
  268.         Call g_d3d.GetAdapterDisplayMode(g_lCurrentAdapter, d3ddm)
  269.         
  270.         ' figure out if this format supports hardware acceleration
  271.         ' by looking it up in our format list
  272.         g_behaviorflags = D3DEnum_FindInFormatList(g_lCurrentAdapter, devtype, d3ddm.format)
  273.         If g_behaviorflags <= 0 Then g_behaviorflags = D3DCREATE_SOFTWARE_VERTEXPROCESSING
  274.         
  275.         
  276.         g_d3dpp.BackBufferFormat = d3ddm.format
  277.         g_d3dpp.BackBufferWidth = 0
  278.         g_d3dpp.BackBufferHeight = 0
  279.         
  280.         g_d3dpp.Windowed = 1
  281.         g_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD
  282.         
  283.                 
  284.         .bWindowed = True
  285.         .DeviceType = devtype
  286.         
  287.         g_devType = devtype
  288.        
  289.                 
  290.         
  291.         
  292.     End With
  293.             
  294.     'Try to create the device now that we have everything set.
  295.     On Local Error Resume Next
  296.     Set g_dev = g_d3d.CreateDevice(g_lCurrentAdapter, devtype, g_focushwnd, g_behaviorflags, g_d3dpp)
  297.  
  298.     If Err.Number Then
  299.  
  300.         If bTryFallbacks = False Then Exit Function
  301.         
  302.         'If a HAL device was being attempted, try again with a REF device instead.
  303.         If g_devType = D3DDEVTYPE_HAL Then
  304.             Err.Clear
  305.  
  306.             'Make sure the user knows that this is less than an optimal 3D environment.
  307.             MsgBox "No hardware support found. Switching to reference rasterizer.", vbInformation
  308.             
  309.             'reset our variable to use ref
  310.             g_Adapters(g_lCurrentAdapter).DeviceType = D3DDEVTYPE_REF
  311.             g_Adapters(g_lCurrentAdapter).bReference = True
  312.             g_devType = D3DDEVTYPE_REF
  313.             Set g_dev = g_d3d.CreateDevice(g_lCurrentAdapter, D3DDEVTYPE_REF, g_focushwnd, g_behaviorflags, g_d3dpp)
  314.             
  315.         End If
  316.             
  317.     End If
  318.  
  319.     If Err.Number Then
  320.         
  321.         'The app still hit an error. Both HAL and REF devices weren't created. The app will have to exit at this point.
  322.         MsgBox "No suitable device was found to initialize D3D. Application will now exit.", vbCritical
  323.         D3DUtil_InitWindowed = False
  324.         End
  325.         Exit Function
  326.  
  327.     End If
  328.  
  329.     'update our device caps data
  330.     g_dev.GetDeviceCaps g_d3dCaps
  331.     
  332.     'set any state we need to initialize
  333.     D3DXMatrixIdentity g_identityMatrix
  334.     
  335.     'set the reference flag if we choose a ref device
  336.     With g_Adapters(g_lCurrentAdapter)
  337.         If .DeviceType = D3DDEVTYPE_REF Then
  338.             .bReference = True
  339.         Else
  340.             .bReference = False
  341.         End If
  342.     End With
  343.     
  344.     D3DUtil_InitWindowed = True
  345.     Exit Function
  346.     
  347. errOut:
  348.     Debug.Print "Failed D3DUtil_Init"
  349.  
  350. End Function
  351.  
  352.  
  353. '-----------------------------------------------------------------------------
  354. 'DOC: D3DUtil_InitFullscreen
  355. 'DOC:   This function creates the following objects: DirectX8, Direct3D8,
  356. 'DOC:   Direc3DDevice8.
  357. 'DOC:
  358. 'DOC: Params:
  359. 'DOC:
  360. 'DOC:   hwnd            starting hwnd to display graphics in
  361. 'DOC:                   This can be changed with a call to Reset if drawing
  362. 'DOC:                   to multiple windows
  363. 'DOC:                   For full screen operation the hwnd will have to belong
  364. 'DOC:                   to a top level window
  365. 'DOC:
  366. 'DOC:   AdapterIndex    The index of the display card to be used (most often 0)
  367. 'DOC:
  368. 'DOC:   ModeIndex       index into the g_adapters(AdapterIndex).Modes for width
  369. 'DOC:                   height and format
  370. 'DOC:
  371. 'DOC:   DevType         Indicates if user wants HAL (hardware) or REF (software) rendering
  372. 'DOC:
  373. 'DOC:   bTryFallbacks   True if wants function to attempt to fallback to the reference device
  374. 'DOC:                   on faulure and display dialogs to that effect
  375. 'DOC:
  376. '-----------------------------------------------------------------------------
  377. Function D3DUtil_InitFullscreen(hwnd As Long, AdapterIndex As Long, modeIndex As Long, devtype As CONST_D3DDEVTYPE, bTryFallbacks As Boolean) As Boolean
  378.     
  379.     On Error GoTo errOut
  380.     
  381.     Dim ModeInfo As D3DUTIL_MODEINFO
  382.  
  383.     Dim rc As RECT
  384.     
  385.     'save the current adapter
  386.     g_lCurrentAdapter = AdapterIndex
  387.         
  388.     
  389.     g_focushwnd = hwnd
  390.     
  391.     
  392.     'switching from windowed to fullscreen so save height and width
  393.     If g_d3dpp.Windowed = 1 Then
  394.         Call GetClientRect(g_focushwnd, rc)
  395.         g_lWindowWidth = rc.Right - rc.Left
  396.         g_lWindowHeight = rc.bottom - rc.Top
  397.         Call GetWindowRect(g_focushwnd, g_WindowRect)
  398.     End If
  399.  
  400.     
  401.     ' Initialize the present parameters structure
  402.     ' to use 1 back buffer and a 16 bit depth buffer
  403.     ' change the autoDepthStencilFormat if you need stencil bits
  404.     With g_d3dpp
  405.         .BackBufferCount = 1
  406.         .EnableAutoDepthStencil = 1
  407.         .AutoDepthStencilFormat = D3DFMT_D16
  408.         .hDeviceWindow = g_focushwnd
  409.     End With
  410.     
  411.     
  412.     
  413.     
  414.     With g_Adapters(g_lCurrentAdapter)
  415.             
  416.         With .DevTypeInfo(devtype)
  417.             ModeInfo = .Modes(modeIndex)
  418.             g_behaviorflags = .Modes(modeIndex).VertexBehavior
  419.             
  420.             g_d3dpp.BackBufferWidth = ModeInfo.lWidth
  421.             g_d3dpp.BackBufferHeight = ModeInfo.lHeight
  422.             g_d3dpp.BackBufferFormat = ModeInfo.format
  423.             g_d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP
  424.             g_d3dpp.Windowed = 0
  425.             .lCurrentMode = modeIndex
  426.         End With
  427.              
  428.  
  429.         .bWindowed = False
  430.         .DeviceType = devtype
  431.         
  432.         
  433.         If g_behaviorflags = 0 Then g_behaviorflags = D3DCREATE_SOFTWARE_VERTEXPROCESSING
  434.         g_devType = devtype
  435.         
  436.     End With
  437.             
  438.     'Try to create the device now that we have everything set.
  439.     On Local Error Resume Next
  440.     Set g_dev = g_d3d.CreateDevice(g_lCurrentAdapter, devtype, g_focushwnd, g_behaviorflags, g_d3dpp)
  441.  
  442.     If Err.Number Then
  443.         If bTryFallbacks = False Then Exit Function
  444.  
  445.         'If a HAL device was being attempted, try again with a REF device instead.
  446.         If g_devType = D3DDEVTYPE_HAL Then
  447.             Err.Clear
  448.  
  449.             'Make sure the user knows that this is less than an optimal 3D environment.
  450.             MsgBox "No hardware support found. Switching to reference rasterizer.", vbInformation
  451.             
  452.             'reset our variable to use ref
  453.             g_Adapters(g_lCurrentAdapter).DeviceType = D3DDEVTYPE_REF
  454.             g_devType = D3DDEVTYPE_REF
  455.             Set g_dev = g_d3d.CreateDevice(g_lCurrentAdapter, D3DDEVTYPE_REF, g_focushwnd, g_behaviorflags, g_d3dpp)
  456.             
  457.         End If
  458.             
  459.     End If
  460.  
  461.  
  462.     If Err.Number Then
  463.         
  464.         'The app still hit an error. Both HAL and REF devices weren't created. The app will have to exit at this point.
  465.         MsgBox "No suitable device was found to initialize D3D. Application will now exit.", vbCritical
  466.         D3DUtil_InitFullscreen = False
  467.         End
  468.         Exit Function
  469.  
  470.     End If
  471.  
  472.     'update our device caps data
  473.     g_dev.GetDeviceCaps g_d3dCaps
  474.     
  475.     'set any state we need to initialize
  476.     D3DXMatrixIdentity g_identityMatrix
  477.     
  478.     'set the reference flag if we choose a ref device
  479.     With g_Adapters(g_lCurrentAdapter)
  480.         If .DeviceType = D3DDEVTYPE_REF Then
  481.            .bReference = True
  482.         Else
  483.            .bReference = False
  484.         End If
  485.     End With
  486.     D3DUtil_InitFullscreen = True
  487.     Exit Function
  488.     
  489. errOut:
  490.     Debug.Print "Failed D3DUtil_InitFullscreen"
  491. End Function
  492.  
  493.  
  494. '-----------------------------------------------------------------------------
  495. 'DOC: D3DUtil_ResetWindowed
  496. 'DOC:
  497. 'DOC: Remarks
  498. 'DOC:   Used to move out of Fullscreen mode to windowed mode with out changing
  499. 'DOC:   the current device
  500. '-----------------------------------------------------------------------------
  501. Function D3DUtil_ResetWindowed() As Long
  502.         Dim ws As Long
  503.         Dim d3dppnew As D3DPRESENT_PARAMETERS
  504.         
  505.         On Local Error GoTo errOut
  506.         
  507.         d3dppnew.Windowed = 1
  508.         d3dppnew.BackBufferFormat = g_Adapters(g_lCurrentAdapter).DesktopMode.format
  509.         d3dppnew.EnableAutoDepthStencil = g_d3dpp.EnableAutoDepthStencil
  510.         d3dppnew.AutoDepthStencilFormat = g_d3dpp.AutoDepthStencilFormat
  511.         d3dppnew.SwapEffect = D3DSWAPEFFECT_DISCARD
  512.         d3dppnew.hDeviceWindow = g_d3dpp.hDeviceWindow
  513.         
  514.         g_dev.Reset d3dppnew
  515.         
  516.         g_d3dpp = d3dppnew
  517.         
  518.         Const GWL_EXSTYLE = -20
  519.         Const GWL_STYLE = -16
  520.         Const WS_EX_TOPMOST = 8
  521.         Const HWND_NOTOPMOST = -2
  522.             
  523.                 
  524.         With g_WindowRect
  525.             Call SetWindowPos(g_focushwnd, HWND_NOTOPMOST, .Left, .Top, .Right - .Left, .bottom - .Top, 0)
  526.             ws = GetWindowLongA(g_focushwnd, GWL_STYLE)
  527.             If (ws And WS_EX_TOPMOST) = WS_EX_TOPMOST Then
  528.                 ws = ws - WS_EX_TOPMOST
  529.                 Call SetWindowLongA(g_focushwnd, GWL_STYLE, ws)
  530.             End If
  531.         End With
  532.         
  533.         
  534.         DoEvents
  535.         D3DUtil_ResetWindowed = 0
  536.         Exit Function
  537. errOut:
  538.     D3DUtil_ResetWindowed = Err.Number
  539.     Debug.Print "err in ResetWindow"
  540. End Function
  541.  
  542.  
  543. '-----------------------------------------------------------------------------
  544. 'DOC: D3DUtil_ResetFullscreen
  545. 'DOC:
  546. 'DOC: Remarks
  547. 'DOC:   Used to to toggle from windowed mode to the current fullscreen mode
  548. 'DOC:   Without changing the current device
  549. '-----------------------------------------------------------------------------
  550. Function D3DUtil_ResetFullscreen() As Long
  551.         Dim hr As Long
  552.         Dim lMode As Long
  553.         Dim rc As RECT
  554.         Dim devtype As CONST_D3DDEVTYPE
  555.         On Local Error GoTo errOut
  556.         If g_d3dpp.Windowed = 1 Then
  557.             Call GetClientRect(g_focushwnd, rc)
  558.             g_lWindowWidth = rc.Right - rc.Left
  559.             g_lWindowHeight = rc.bottom - rc.Top
  560.             Call GetWindowRect(g_focushwnd, g_WindowRect)
  561.         End If
  562.         
  563.         devtype = g_Adapters(g_lCurrentAdapter).DeviceType
  564.         With g_Adapters(g_lCurrentAdapter).DevTypeInfo(devtype)
  565.             g_d3dpp.Windowed = 0
  566.             g_d3dpp.BackBufferWidth = .Modes(.lCurrentMode).lWidth
  567.             g_d3dpp.BackBufferHeight = .Modes(.lCurrentMode).lHeight
  568.             g_d3dpp.BackBufferFormat = .Modes(.lCurrentMode).format
  569.         End With
  570.         
  571.         g_dev.Reset g_d3dpp
  572. errOut:
  573.         D3DUtil_ResetFullscreen = Err.Number
  574. End Function
  575.  
  576. '-----------------------------------------------------------------------------
  577. 'DOC: D3DEnum_BuildAdapterList
  578. 'DOC:   Used to intialzed a list of valid adapters and display modes
  579. 'DOC:
  580. 'DOC: Params:
  581. 'DOC:   EnumCallback    - can be Nothing or an object that has implemented
  582. 'DOC:                     VerifyDevice(usageflags as long ,format as CONST_D3DFORMAT)
  583. 'DOC:                     ussgeflags can be
  584. 'DOC:                           D3DCREATE_SOFTWARE_VERTEXPROCESSING
  585. 'DOC:                           D3DCREATE_HARDWARE_VERTEXPROCESSING
  586. 'DOC: Remarks:
  587. 'DOC:   caps for the device are passed to VerifyDevice in the g_d3dcaps global
  588. 'DOC:
  589. '-----------------------------------------------------------------------------
  590. Public Function D3DEnum_BuildAdapterList(EnumCallback As Object) As Boolean
  591.     
  592.     On Local Error GoTo errOut
  593.     
  594.     Dim lAdapter As Long
  595.         
  596.     ' empty the list
  597.     Call D3DEnum_Cleanup
  598.             
  599.     ' create d3d and dx objects if not already created
  600.     If g_dx Is Nothing Then Set g_dx = New DirectX8
  601.     If g_d3d Is Nothing Then Set g_d3d = g_dx.Direct3DCreate
  602.     If g_d3dx Is Nothing Then Set g_d3dx = New D3DX8
  603.     
  604.     ' save callback
  605.     Set g_EnumCallback = EnumCallback
  606.     
  607.     ' Make space for new adapter
  608.     g_lNumAdapters = g_d3d.GetAdapterCount
  609.     ReDim g_Adapters(g_lNumAdapters)
  610.     
  611.     ' Loop through all the adapters on the system
  612.     For lAdapter = 0 To g_lNumAdapters - 1
  613.     
  614.         ' build a list of valid backbuffer formats
  615.         D3DEnum_BuildValidFormatList lAdapter, D3DDEVTYPE_HAL
  616.         D3DEnum_BuildValidFormatList lAdapter, D3DDEVTYPE_REF
  617.         
  618.                 
  619.         ' build a list of valid display modes for those formats
  620.         D3DEnum_BuildDisplayModeList lAdapter, D3DDEVTYPE_HAL
  621.         D3DEnum_BuildDisplayModeList lAdapter, D3DDEVTYPE_REF
  622.         
  623.         ' get the adapter identifier
  624.         g_d3d.GetAdapterIdentifier lAdapter, 0, g_Adapters(lAdapter).d3dai
  625.         
  626.     Next
  627.     
  628.     D3DEnum_BuildAdapterList = True
  629.     Exit Function
  630.     
  631. errOut:
  632.     Debug.Print "Failed D3DEnum_BuildAdapterList"
  633. End Function
  634.  
  635.  
  636.  
  637. '-----------------------------------------------------------------------------
  638. 'DOC:  D3DUtil_ResizeWindowed
  639. 'DOC:
  640. 'DOC:  Paramters
  641. 'DOC:       hWnd device window
  642. 'DOCL
  643. 'DOC:  Remarks
  644. 'DOC:       use when already in windowed mode to resize the backbuffer
  645. 'DOC:       do not use to switch from fullscreen to windowed mode
  646. '-----------------------------------------------------------------------------
  647. Function D3DUtil_ResizeWindowed(hwnd As Long) As Boolean
  648.     On Local Error GoTo errOut
  649.     
  650.     Dim d3dpp As D3DPRESENT_PARAMETERS
  651.     Dim rc As RECT
  652.     
  653.     d3dpp = g_d3dpp
  654.     
  655.     If d3dpp.Windowed = 0 Then Exit Function
  656.     
  657.     g_focushwnd = hwnd
  658.     Call GetClientRect(g_focushwnd, rc)
  659.     g_lWindowWidth = rc.Right - rc.Left
  660.     g_lWindowHeight = rc.bottom - rc.Top
  661.     Call GetWindowRect(g_focushwnd, g_WindowRect)
  662.     
  663.         
  664.     
  665.     d3dpp.BackBufferWidth = 0 'g_lWindowWidth
  666.     d3dpp.BackBufferHeight = 0 'g_lWindowHeight
  667.     d3dpp.hDeviceWindow = hwnd
  668.     d3dpp.Windowed = 1
  669.                
  670.     g_dev.Reset d3dpp
  671.     
  672.     g_d3dpp = d3dpp
  673.     
  674.     
  675.     g_Adapters(g_lCurrentAdapter).bWindowed = True
  676.  
  677.     D3DUtil_ResizeWindowed = True
  678.     Exit Function
  679.     
  680. errOut:
  681.     Debug.Print "D3DUtil_ResizeWindowed failed - make sure width and height are in pixels"
  682.     If Err.Number <> 0 Then 'The call to reset failed.
  683.         D3DUtil_ResizeWindowed = False
  684.         MsgBox "Could not reset the Direct3D Device." & vbCrLf & "This sample will now exit.", vbOKOnly Or vbCritical, "Failure"
  685.         End
  686.         Exit Function
  687.     End If
  688. End Function
  689.  
  690.  
  691. '-----------------------------------------------------------------------------
  692. 'DOC:  D3DUtil_ResizeFullscreen
  693. 'DOC:
  694. 'DOC:  Paramters
  695. 'DOC:       hWnd device window
  696. 'DOC:       modeIndex index into Modes list
  697. 'DOC:
  698. 'DOC:  Remarks
  699. 'DOC:       D3DUtil_Init or D3DEnum_BuildAdapterList must have been called
  700. 'DOC:       prior to call D3DUtil_ResizeFullscreen
  701. 'DOC:       Use this method when moving from windowed mode to fullscreen
  702. 'DOC:       on the current device
  703. 'DOC:       Note that all device state is lost and that the caller
  704. 'DOC:       will need to call ther RestoreDeviceObjects function
  705. 'DOC:
  706. '-----------------------------------------------------------------------------
  707. Function D3DUtil_ResizeFullscreen(hwnd As Long, modeIndex As Long) As Boolean
  708.     On Local Error GoTo errOut
  709.     
  710.     Dim d3dpp As D3DPRESENT_PARAMETERS
  711.     Dim devtype As CONST_D3DDEVTYPE
  712.     Dim prevmode As Long
  713.     
  714.     'let ResizeWindowed know we are trying to go fullscreen
  715.     prevmode = g_d3dpp.Windowed
  716.     g_d3dpp.Windowed = 0
  717.         
  718.     devtype = g_Adapters(g_lCurrentAdapter).DeviceType
  719.     With g_Adapters(g_lCurrentAdapter).DevTypeInfo(devtype).Modes(modeIndex)
  720.         d3dpp.BackBufferWidth = .lWidth
  721.         d3dpp.BackBufferHeight = .lHeight
  722.         d3dpp.BackBufferFormat = .format
  723.         d3dpp.hDeviceWindow = hwnd
  724.         d3dpp.AutoDepthStencilFormat = g_d3dpp.AutoDepthStencilFormat
  725.         d3dpp.EnableAutoDepthStencil = g_d3dpp.EnableAutoDepthStencil
  726.         d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP
  727.         d3dpp.Windowed = 0
  728.     End With
  729.     
  730.     g_dev.Reset d3dpp
  731.     
  732.     g_d3dpp = d3dpp
  733.     
  734.     'reset succeeded so set new behavior flags
  735.     With g_Adapters(g_lCurrentAdapter)
  736.         g_behaviorflags = .DevTypeInfo(devtype).Modes(modeIndex).VertexBehavior
  737.         .bWindowed = False
  738.     End With
  739.     
  740.     D3DUtil_ResizeFullscreen = True
  741.     Exit Function
  742.     
  743. errOut:
  744.     'we where unsuccessfull in going fullscreen
  745.     'indicate we are still in previous mode
  746.     g_d3dpp.Windowed = prevmode
  747.     Debug.Print "D3DUtil_ResizeWindowed failed - make sure width and height are in pixels"
  748. End Function
  749.  
  750.  
  751. '-----------------------------------------------------------------------------
  752. 'DOC: D3DUtil_DefaultInitWindowed
  753. 'DOC:   Used to intialzed D3DUtil device in a windowed mode
  754. 'DOC  Params:
  755. 'DOC:   iAdapter    DisplayAdapter ordinal
  756. 'DOC:   hwnd        Display hwnd
  757. 'DOC: Remarks:
  758. 'DOC:
  759. 'DOC:   Users can initialze the g_d3d and g_dev objects themselves
  760. 'DOC:   and not use this function be sure to initialize
  761. 'DOC:       g_iAdapter, g_devType,g_behaviorFlags,g_focushwnd,g_presentParams
  762. 'DOC:
  763. 'DOC:   This function defaults to using SOFTWARE_VERTEXPROCESSING
  764. 'DOC:   and requires HAL 3d support
  765. '-----------------------------------------------------------------------------
  766.  
  767. Public Function D3DUtil_DefaultInitWindowed(iAdapter As Long, hwnd As Long) As Boolean
  768.     On Local Error GoTo errOut
  769.     
  770.     Dim emptyparams As D3DPRESENT_PARAMETERS
  771.     
  772.     If g_dx Is Nothing Then Set g_dx = New DirectX8
  773.     If g_d3dx Is Nothing Then Set g_d3dx = New D3DX8
  774.     
  775.     If g_d3d Is Nothing Then Set g_d3d = g_dx.Direct3DCreate
  776.     
  777.     
  778.     g_lCurrentAdapter = iAdapter
  779.     g_devType = D3DDEVTYPE_HAL
  780.     g_behaviorflags = D3DCREATE_SOFTWARE_VERTEXPROCESSING
  781.     g_focushwnd = hwnd
  782.     g_d3dpp = emptyparams
  783.     
  784.     Dim dm As D3DDISPLAYMODE
  785.     
  786.     g_d3d.GetAdapterDisplayMode iAdapter, dm
  787.     
  788.     
  789.     With g_d3dpp
  790.         .BackBufferFormat = dm.format
  791.         .EnableAutoDepthStencil = 1 'TRUE
  792.         .AutoDepthStencilFormat = D3DFMT_D16
  793.         .Windowed = 1   'TRUE
  794.         .SwapEffect = D3DSWAPEFFECT_DISCARD
  795.     End With
  796.             
  797.             
  798.     Set g_dev = g_d3d.CreateDevice(iAdapter, g_devType, g_focushwnd, g_behaviorflags, g_d3dpp)
  799.     
  800.     g_dev.GetDeviceCaps g_d3dCaps
  801.     
  802.     D3DUtil_DefaultInitWindowed = True
  803.     Exit Function
  804.     
  805. errOut:
  806. End Function
  807.  
  808.  
  809. '-----------------------------------------------------------------------------
  810. 'DOC: D3DUtil_DefaultInitFullscreen
  811. 'DOC:   Used to intialzed D3DUtil device in a windowed mode
  812. 'DOC  Params:
  813. 'DOC:   iAdapter    DisplayAdapter ordinal
  814. 'DOC:   hwnd        Display hwnd
  815. 'DOC:   w           width
  816. 'DOC    h           height
  817. 'DOC    fmt         desired format
  818. 'DOC: Remarks:
  819. 'DOC:
  820. 'DOC:   Users can initialze the g_d3d and g_dev objects themselves
  821. 'DOC:   and not use this function be sure to initialize
  822. 'DOC:       g_iAdapter, g_devType,g_behaviorFlags,g_focushwnd,g_presentParams
  823. 'DOC:
  824. 'DOC:   This function defaults to using SOFTWARE_VERTEXPROCESSING
  825. 'DOC:   and requires HAL 3d support
  826. '-----------------------------------------------------------------------------
  827.  
  828. Public Function D3DUtil_DefaultInitFullscreen(iAdapter As Long, hwnd As Long, w As Long, h As Long, fmt As CONST_D3DFORMAT) As Boolean
  829.     On Local Error GoTo errOut
  830.     
  831.     Dim emptyparams As D3DPRESENT_PARAMETERS
  832.     
  833.     If g_dx Is Nothing Then Set g_dx = New DirectX8
  834.     If g_d3dx Is Nothing Then Set g_d3dx = New D3DX8
  835.     
  836.     If g_d3d Is Nothing Then Set g_d3d = g_dx.Direct3DCreate
  837.     
  838.     
  839.     g_lCurrentAdapter = iAdapter
  840.     g_devType = D3DDEVTYPE_HAL
  841.     g_behaviorflags = D3DCREATE_SOFTWARE_VERTEXPROCESSING
  842.     g_focushwnd = hwnd
  843.     g_d3dpp = emptyparams
  844.     
  845.     Dim dm As D3DDISPLAYMODE
  846.     
  847.     g_d3d.GetAdapterDisplayMode iAdapter, dm
  848.     
  849.     
  850.     With g_d3dpp
  851.         .BackBufferFormat = fmt
  852.         .EnableAutoDepthStencil = 1 'TRUE
  853.         .AutoDepthStencilFormat = D3DFMT_D16
  854.         .BackBufferWidth = w
  855.         .BackBufferHeight = h
  856.         .Windowed = 0   'FALSE
  857.         .SwapEffect = D3DSWAPEFFECT_FLIP
  858.     End With
  859.             
  860.             
  861.     Set g_dev = g_d3d.CreateDevice(iAdapter, g_devType, g_focushwnd, g_behaviorflags, g_d3dpp)
  862.     
  863.     g_dev.GetDeviceCaps g_d3dCaps
  864.     
  865.     D3DUtil_DefaultInitFullscreen = True
  866.     Exit Function
  867.     
  868. errOut:
  869. End Function
  870.  
  871.  
  872.  
  873.  
  874.  
  875. '-----------------------------------------------------------------------------
  876. 'DOC: D3DEnum_Cleanup
  877. 'DOC:   Used to release any reference to the callback object passed in
  878. 'DOC:   and deallocate memory
  879. 'DOC: Params:
  880. 'DOC:   none
  881. 'DOC: Remarks:
  882. 'DOC:   none
  883. '-----------------------------------------------------------------------------
  884. Public Sub D3DEnum_Cleanup()
  885.     Set g_EnumCallback = Nothing
  886.     ReDim g_Adapters(0)
  887. End Sub
  888.  
  889.  
  890. '------------------------------------------------------------------
  891. ' Private Functions
  892. '------------------------------------------------------------------
  893.  
  894. '-----------------------------------------------------------------------------
  895. ' D3DEnum_BuildValidFormatList
  896. '-----------------------------------------------------------------------------
  897. Private Sub D3DEnum_BuildValidFormatList(lAdapter As Long, devtype As CONST_D3DDEVTYPE)
  898.                         
  899.         Dim lMode As Long
  900.         Dim lUsage As Long
  901.         Dim NumModes As Long
  902.         Dim DisplayMode As D3DDISPLAYMODE
  903.         Dim bCanDoWindowed As Boolean
  904.         Dim bCanDoFullscreen As Boolean
  905.                 
  906.         
  907.         With g_Adapters(lAdapter).DevTypeInfo(devtype)
  908.         
  909.             ' Reset the number of available formats to
  910.             .lNumFormats = 0
  911.         
  912.             ' Get the number of display modes
  913.             ' a display mode is a size and format (ie 640x480 X8R8G8B8 60hz)
  914.             NumModes = g_d3d.GetAdapterModeCount(lAdapter)
  915.             ReDim .FormatInfo(NumModes)
  916.                                 
  917.             ' Loop through all the display modes
  918.             For lMode = 0 To NumModes - 1
  919.                     
  920.                 ' Get information about this adapter in all the modes it supports
  921.                 Call g_d3d.EnumAdapterModes(lAdapter, lMode, DisplayMode)
  922.                                 
  923.                 ' See if the format is already in our format list
  924.                 If -1 <> D3DEnum_FindInFormatList(lAdapter, devtype, DisplayMode.format) Then GoTo Continue
  925.                                     
  926.                 ' Check the compatiblity of the format
  927.                 
  928.                 lUsage = D3DEnum_CheckFormatCompatibility(lAdapter, devtype, DisplayMode.format, bCanDoWindowed, bCanDoFullscreen)
  929.                                                                             
  930.                 ' Usage will come back -1 if VerifyDevice reject format
  931.                 If -1 = lUsage Then GoTo Continue
  932.                 
  933.                 ' Add the valid format and ussage
  934.                 .FormatInfo(.lNumFormats).format = DisplayMode.format
  935.                 .FormatInfo(.lNumFormats).usage = lUsage
  936.                 .FormatInfo(.lNumFormats).bCanDoWindowed = bCanDoWindowed
  937.                 .FormatInfo(.lNumFormats).bCanDoFullscreen = bCanDoFullscreen
  938.                 .lNumFormats = .lNumFormats + 1
  939.  
  940.                                 
  941. Continue:
  942.             Next
  943.             
  944.         End With
  945.  
  946. End Sub
  947.  
  948.  
  949. '-----------------------------------------------------------------------------
  950. ' D3DEnum_BuildDisplayModeList
  951. '-----------------------------------------------------------------------------
  952. Private Sub D3DEnum_BuildDisplayModeList(lAdapter As Long, devtype As CONST_D3DDEVTYPE)
  953.                         
  954.         Dim lMode As Long
  955.         Dim NumModes As Long
  956.         Dim DisplayMode As D3DDISPLAYMODE
  957.  
  958.         With g_Adapters(lAdapter).DevTypeInfo(devtype)
  959.         
  960.             ' Reset the number of validated display modes to 0
  961.             .lNumModes = 0
  962.             
  963.             ' Get the number of display modes
  964.             ' Note this list includes refresh rates
  965.             ' a display mode is a size and format (ie 640x480 X8R8G8B8 60hz)
  966.             NumModes = g_d3d.GetAdapterModeCount(lAdapter)
  967.  
  968.             ' Allocate space for our mode list
  969.             ReDim .Modes(NumModes)
  970.  
  971.             ' Save the format of the desktop for windowed operation
  972.             Call g_d3d.GetAdapterDisplayMode(lAdapter, g_Adapters(lAdapter).DesktopMode)
  973.                                 
  974.             ' Loop through all the display modes
  975.             For lMode = 0 To NumModes - 1
  976.                     
  977.                 ' Get information about this adapter in all the modes it supports
  978.                 Call g_d3d.EnumAdapterModes(lAdapter, lMode, DisplayMode)
  979.                 
  980.                 ' filter out low resolution modes
  981.                 If DisplayMode.width < 640 Or DisplayMode.height < 400 Then GoTo Continue
  982.                 
  983.                 ' filter out modes allready in the list
  984.                 ' that might differ only in refresh rate
  985.                 If -1 <> D3DEnum_FindInDisplayModeList(lAdapter, devtype, DisplayMode) Then GoTo Continue
  986.                 
  987.                 
  988.                 ' filter out modes with formats that arent confirmed to work
  989.                 ' see BuildFormatList and ConfirmFormatList
  990.                 If -1 = D3DEnum_FindInFormatList(lAdapter, devtype, DisplayMode.format) Then GoTo Continue
  991.                                                 
  992.                 ' At this point, the modes format has been validated,
  993.                 ' is not a duplicate refresh rate, and not a low res mode
  994.                 ' Add the mode to the list of working modes for the adapter
  995.                 .Modes(.lNumModes).lHeight = DisplayMode.height
  996.                 .Modes(.lNumModes).lWidth = DisplayMode.width
  997.                 .Modes(.lNumModes).format = DisplayMode.format
  998.                 .lNumModes = .lNumModes + 1
  999.                                             
  1000. Continue:
  1001.             Next
  1002.             
  1003.         End With
  1004.  
  1005. End Sub
  1006.  
  1007. '-----------------------------------------------------------------------------
  1008. ' D3DEnum_FindInDisplayModeList
  1009. '-----------------------------------------------------------------------------
  1010. Private Function D3DEnum_FindInDisplayModeList(lAdapter As Long, devtype As CONST_D3DDEVTYPE, DisplayMode As D3DDISPLAYMODE) As Long
  1011.     
  1012.     Dim lMode As Long
  1013.     Dim NumModes As Long
  1014.     
  1015.     NumModes = g_Adapters(lAdapter).DevTypeInfo(devtype).lNumModes
  1016.     D3DEnum_FindInDisplayModeList = -1
  1017.     
  1018.     For lMode = 0 To NumModes - 1
  1019.       With g_Adapters(lAdapter).DevTypeInfo(devtype).Modes(lMode)
  1020.           If .lWidth = DisplayMode.width And _
  1021.               .lHeight = DisplayMode.height And _
  1022.               .format = DisplayMode.format Then
  1023.               D3DEnum_FindInDisplayModeList = lMode
  1024.               Exit Function
  1025.           End If
  1026.       End With
  1027.     Next
  1028.     
  1029. End Function
  1030.  
  1031. '-----------------------------------------------------------------------------
  1032. ' D3DEnum_FindInFormatList
  1033. '-----------------------------------------------------------------------------
  1034. Private Function D3DEnum_FindInFormatList(lAdapter As Long, devtype As CONST_D3DDEVTYPE, format As CONST_D3DFORMAT) As Long
  1035.     
  1036.     Dim lFormat As Long
  1037.     Dim NumFormats As Long
  1038.     
  1039.     NumFormats = g_Adapters(lAdapter).DevTypeInfo(devtype).lNumFormats
  1040.     D3DEnum_FindInFormatList = -1
  1041.     
  1042.     For lFormat = 0 To NumFormats - 1
  1043.       With g_Adapters(lAdapter).DevTypeInfo(devtype).FormatInfo(lFormat)
  1044.           If .format = format Then
  1045.              D3DEnum_FindInFormatList = .usage
  1046.              Exit Function
  1047.           End If
  1048.       End With
  1049.     Next
  1050.     
  1051.     D3DEnum_FindInFormatList = -1
  1052.     
  1053. End Function
  1054.  
  1055.  
  1056. '-----------------------------------------------------------------------------
  1057. ' D3DEnum_CheckFormatCompatibility
  1058. '-----------------------------------------------------------------------------
  1059. Private Function D3DEnum_CheckFormatCompatibility(lAdapter As Long, DeviceType As CONST_D3DDEVTYPE, format As CONST_D3DFORMAT, ByRef OutCanDoWindowed As Boolean, ByRef OutCanDoFullscreen As Boolean) As Long
  1060.         On Local Error GoTo errOut
  1061.  
  1062.         D3DEnum_CheckFormatCompatibility = -1
  1063.         
  1064.         Dim d3dcaps As D3DCAPS8
  1065.         Dim flags As Long
  1066.  
  1067.         ' Filter out incompatible backbuffers
  1068.         ' Note: framework always has the backbuffer and the frontbuffer (screen) format matching
  1069.         OutCanDoWindowed = True: OutCanDoFullscreen = True
  1070.         If 0 <> g_d3d.CheckDeviceType(lAdapter, DeviceType, format, format, 0) Then OutCanDoWindowed = False
  1071.         If 0 <> g_d3d.CheckDeviceType(lAdapter, DeviceType, format, format, 1) Then OutCanDoFullscreen = False
  1072.         If (OutCanDoWindowed = False) And (OutCanDoFullscreen = False) Then Exit Function
  1073.  
  1074.         ' If no form was passed in to use as a callback
  1075.         ' then default to sofware vertex processing
  1076.  
  1077.         ' Get the device capablities
  1078.         g_d3d.GetDeviceCaps lAdapter, DeviceType, g_d3dCaps
  1079.         g_Adapters(lAdapter).d3dcaps = g_d3dCaps
  1080.         
  1081.         ' If user doesnt want to verify the device (didnt provide a callback)
  1082.         ' fall back to software
  1083.         D3DEnum_CheckFormatCompatibility = D3DCREATE_SOFTWARE_VERTEXPROCESSING
  1084.         If g_EnumCallback Is Nothing Then Exit Function
  1085.         
  1086.         ' Confirm the device for HW vertex processing
  1087.         flags = D3DCREATE_HARDWARE_VERTEXPROCESSING
  1088.         D3DEnum_CheckFormatCompatibility = flags
  1089.         If g_d3dCaps.DevCaps And D3DDEVCAPS_HWTRANSFORMANDLIGHT Then
  1090.            If g_EnumCallback.VerifyDevice(flags, format) Then Exit Function
  1091.         End If
  1092.         
  1093.         ' Try Software VertexProcesing
  1094.         flags = D3DCREATE_SOFTWARE_VERTEXPROCESSING
  1095.         D3DEnum_CheckFormatCompatibility = flags
  1096.         If g_EnumCallback.VerifyDevice(flags, format) Then Exit Function
  1097.                                 
  1098.         ' Fail
  1099.         D3DEnum_CheckFormatCompatibility = -1
  1100.         
  1101.         Exit Function
  1102. errOut:
  1103.  
  1104. End Function
  1105.  
  1106.  
  1107.  
  1108.